package com.ifp.weixin.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ifp.weixin.MessageMng.Service.LocationMessageService;
import com.ifp.weixin.biz.core.CoreService;
import com.ifp.weixin.util.SignUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;

@Controller
@RequestMapping("/weixinCore")
public class WeixinController {
	
	@Resource(name="coreService")
	public CoreService coreService;
	
	@Resource(name="locationMessageService")
	public LocationMessageService locationMessageService;
	
	@RequestMapping(method = RequestMethod.GET)
	public void get(HttpServletRequest request, HttpServletResponse response) {
		// 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
		System.out.println(new Date() +": recived get http msg!  " );
		coreService.haha();
		String signature = request.getParameter("signature");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");
		// 随机字符串
		String echostr = request.getParameter("echostr");
		
		

		PrintWriter out = null;
		try {
			out = response.getWriter();
			// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，否则接入失败
			if (SignUtil.checkSignature(signature, timestamp, nonce)) {
				out.print(echostr);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.close();
			out = null;
		}
	}

	@RequestMapping(method = RequestMethod.POST)
	public void post(HttpServletRequest request, HttpServletResponse response) {
		
		System.out.println(new Date() +"recived post http msg!  " );
		coreService.haha();
		try {
			System.out.println("111111111!");
			request.setCharacterEncoding("UTF-8");
			System.out.println("222222222!");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println("333333333!");
		response.setCharacterEncoding("UTF-8");
		System.out.println("444444444!");
		// 调用核心业务类接收消息、处理消息
		String respMessage = coreService.processRequest(request);

		// 响应消息
		PrintWriter out = null;
		if ( respMessage !=null && !respMessage.equals("")) {
			try {
				out = response.getWriter();
				out.print(respMessage);
				System.out.println(new Date() +" : respMessage =  "+ respMessage );
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				out.close();
				out = null;
			}
		} else {
			try {
				StringBuffer sb= new StringBuffer();
				sb.append( "对不起系统异常！请稍后再试；" );
					
				respMessage = sb.toString();
				out = response.getWriter();
				out.print(respMessage);
				
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				out.close();
				out = null;
			}
		}
	}
	



}